﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace TestConsoleApp
{
    public class TestVolatile
    {
        private int n;

        private bool isDeal;

        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentThread();

        [DllImport("kernel32.dll")]
        static extern IntPtr SetThreadAffinityMask(IntPtr hThread, UIntPtr dwThreadAffinityMask);


        public void Start()
        {
            try
            {
                int num = 0;

                while (true)
                {
                    num++;

                    Thread thread = new Thread(() =>
                    {
                        try
                        {
                            Thread.BeginThreadAffinity();
                            SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(SetCpuID(1)));
                            n++;
                            //isDeal = true;
                        }
                        finally
                        {
                            Thread.EndThreadAffinity();
                        }

                    });


                    Thread thread1 = new Thread(() =>
                    {
                        try
                        {
                            //Stopwatch stopWatch = new Stopwatch();
                            //stopWatch.Start();
                            Thread.BeginThreadAffinity();
                            SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(SetCpuID(2)));

                            //int i = 0;
                            //while (true)
                            //{
                            //    if (isDeal)
                            //    {
                            //        if (n == 0)
                            //        {
                            //            Console.WriteLine("执行循环：" + num);

                            //            throw new Exception("qx");
                            //        }
                            //        //Console.WriteLine(n);
                            //        //stopWatch.Stop();
                            //        //Console.WriteLine(stopWatch.ElapsedMilliseconds);
                            //        return;
                            //    }

                            //    //i++;
                            //}
                            n--;
                        }
                        finally
                        {
                            Thread.EndThreadAffinity();
                        }
                    });


                    thread.Start();
                    thread1.Start();
                    thread.Join();
                    thread1.Join();


                    if (n != 0)
                    {
                        Console.WriteLine("执行循环：" + num);
                    }
                }

            }
            catch (Exception ex)
            {


            }


        }

        int count = 0;
        int MAX = 1000000;
        object islock = new object();

        public void Start1()
        {
            System.Threading.Tasks.Parallel.For(0, MAX, (i) =>
            {
                Interlocked.Increment(ref count);
            });

            Console.WriteLine(count);
            Console.Read();
        }

        ulong SetCpuID(int id)
        {
            ulong cpuid = 0;
            if (id < 0 || id >= System.Environment.ProcessorCount)
            {
                id = 0;
            }
            cpuid |= 1UL << id;
            return cpuid;
        }
    }
}
